package com.ly.nethub

import com.ly.nethub.utils.UUIDUtils
import io.netty.buffer.ByteBufAllocator
import org.junit.Assert.assertEquals
import org.junit.Test
import java.util.concurrent.PriorityBlockingQueue
import java.util.concurrent.atomic.AtomicBoolean


/**
 * Example local unit test, which will execute on the development machine (host).
 *
 * See [testing documentation](http://d.android.com/tools/testing).
 */
@ExperimentalStdlibApi
class ExampleUnitTest {
    @Test
    fun addition_isCorrect() {
        val regex = Regex("^([1-9]\\d{0,3})|^0")
        assertEquals(false, regex.matches("92345"))
        assertEquals(true, regex.matches("1"))
        assertEquals(false, regex.matches("111111"))
        assertEquals(true, regex.matches("8080"))
    }

    @Test
    fun eachTest() {
        val uuids = mutableListOf<String>()
        for (i in 0..100000) {
            val generate = UUIDUtils.generateShortUuid()
            if (uuids.contains(generate)) {
                println("存在一模一样的UUID:$generate")
            } else {
                uuids.add(generate)
            }
        }
    }

    @Test
    fun byteTest() {
        val hexString =
            "6a3e3e6a87010000010000010e050000017b22636d64223a3236302c2264617461223a7b22726573756c74223a312c22746f706963223a226c6c696f742f66696964735f7265706f72742f3030306232303132386366363831666666656162643336322f32222c227061796c6f6164223a7b226d6574686f64223a22646d67722e6e6f746966794649494453222c22737263223a223030303132303034313463396366346534626336222c22647374223a223030303030303031303030303030303030303030222c2276657273696f6e223a2256312e30222c22706172616d73223a7b22646972656374446964223a223030303132303034313463396366346534626336222c22646964223a22303030623230313238636636383166666665616264333632222c2273696964223a322c226669696473223a5b7b2266696964223a31363437332c2276616c7565223a7b227374617465223a317d7d2c7b2266696964223a31363437322c2276616c7565223a7b227374617465223a317d7d5d7d2c22736571223a343231357d7d7d43"
        val list = hexString.chunked(2) { it.toString().toInt(16).toByte() }
        println("hexString:${list.size}")
        val byteArray = list.toByteArray()
        // 创建一个 ByteBufAllocator 对象
        val allocator = ByteBufAllocator.DEFAULT
        // 使用 allocator 创建一个 ByteBuf 对象
        val byteBuf = allocator.buffer() // 创建一个堆缓冲区
        byteBuf.writeBytes(byteArray)
        // 输出 ByteBuf 中的数据
        byteBuf.readByte()
        byteBuf.readByte()
        byteBuf.readByte()
        byteBuf.readByte()
        println(byteBuf.readIntLE())
        val twoBytes = byteBuf.readBytes(2)
        println("${twoBytes.order()},${twoBytes.getByte(0)},${twoBytes.getByte(1)}")
        byteBuf.readByte()
        byteBuf.readByte()
        byteBuf.readIntLE()
        byteBuf.readByte()
        println(byteBuf.readableBytes())
        val content = ByteArray(byteBuf.readableBytes())
        byteBuf.readBytes(content)
        println(String(content).dropLast(1))
        byteBuf.release()
    }

    @Test
    fun queueTest() {
        val queue = PriorityBlockingQueue<Int>()
        queue.offer(6)
        queue.offer(2)
        queue.offer(3)
        println("before:$queue")
        val getItem = queue.take()
        println("poll item:$getItem")
        println("after:$queue")
    }

    @Test
    fun testLooper() {
        val queue = PriorityBlockingQueue<Int>()
        for (i in 0..49) {
            queue.offer(i)
        }
        for (i in 50..100) {
            queue.looper(i)
        }
    }

    private val started = AtomicBoolean(false)

    private fun PriorityBlockingQueue<Int>.looper(value: Int) {
        offer(value)
        if (!started.get()) {
            started.set(true)
            while (true) {
                val item = poll()
                if (item == null) {
                    started.set(false)
                    break
                }
                println("looper item:$item")
            }
        }
    }

    @Test
    fun ipTest() {
        val regex =
            Regex("^(https?://)?(www\\.)?([a-zA-Z0-9-]+(\\.[a-zA-Z]{2,})+)(:\\d+)?(\\/\\S*)?$")
        listOf(
            "https://www.example.com",
            "http://example.com",
            "http://www.example.com:8080/path",
            "https://example.com/path",
            "https://www.example.com/path/to/page.html",
            "invalid-url"
        ).forEach {
            println("validUrl:${regex.matches(it)}")
        }

    }
}
